package cn.bugstack.types.design.framework.tree;


import lombok.Getter;
import lombok.Setter;

/**
 * @author 丨一
 * @version 1.0
 * @title AbstractMultiThreadStrategyRouter
 * @description: 带异步数据加载的策略路由
 * @date 2025/8/11 下午11:34
 */
public abstract class AbstractMultiThreadStrategyRouter<T, D, R> implements StrategyHandler<T, D, R>, StrategyMapper<T, D, R> {

    //默认实例
    @Getter
    @Setter
    protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;

    //策略路由
    public R router(T requestParameter, D dynamicContext) throws Exception {
        StrategyHandler<T, D, R> strategyHandler = this.get(requestParameter, dynamicContext);

        //获取策略 -> 如果没有对应策略,则返回默认的空策略
        if (strategyHandler != null) return strategyHandler.apply(requestParameter, dynamicContext);
        else return defaultStrategyHandler.apply(requestParameter, dynamicContext);
    }

    @Override
    public R apply(T requestParameter, D dynamicContext) throws Exception {
        //异步数据加载
        multiThread(requestParameter, dynamicContext);

        //业务流程受理
        return doApply(requestParameter, dynamicContext);
    }

    //异步数据加载
    protected abstract void multiThread(T requestParameter, D dynamicContext) throws Exception;

    //逻辑受理操作
    protected abstract R doApply(T requestParameter, D dynamicContext) throws Exception;


}
